package com.insurance.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.insurance.common.dto.OrderDto;
import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.common.utils.Query;
import com.insurance.dao.OrderDao;
import com.insurance.entity.OrderEntity;
import com.insurance.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取查询条件对象
     *
     * @return
     */
    public QueryWrapper<OrderEntity> getQueryWrapper(){
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<OrderEntity>();
        queryWrapper.eq("delete_flag", DeleteFlagEnum.UN_DELETE.getCode());
        queryWrapper.orderByDesc("create_time");
        return queryWrapper;
    }

    /**
     * 通过实体类获取查询条件对象
     *
     * @param orderEntity 查询条件
     * @return
     */
    public QueryWrapper<OrderEntity> getQueryWrapper(OrderEntity orderEntity){
        QueryWrapper<OrderEntity> queryWrapper = getQueryWrapper();
        if(!StringUtils.isBlank(orderEntity.getOrderId())){
            queryWrapper.eq("order_id", orderEntity.getOrderId());
        }
        if(!StringUtils.isBlank(orderEntity.getOrderState())){
            queryWrapper.eq("order_state", orderEntity.getOrderState());
        }
        if(!StringUtils.isBlank(orderEntity.getCreatePersonId())){
            queryWrapper.eq("create_person_id", orderEntity.getCreatePersonId());
        }
        if(!StringUtils.isBlank(orderEntity.getAppId())){
            queryWrapper.eq("app_id", orderEntity.getAppId());
        }
        if(!StringUtils.isBlank(orderEntity.getProductId())){
            queryWrapper.eq("product_id", orderEntity.getProductId());
        }
        if(!StringUtils.isBlank(orderEntity.getSerialNumber())){
            queryWrapper.eq("serial_number", orderEntity.getSerialNumber());
        }
        return queryWrapper;
    }

    /**
     * 分页查询订单
     *
     * @param page 分页条件
     * @param orderEntity 查询条件
     * @return
     */
    @Override
    public Page<OrderEntity> queryOrderPage(Page page, OrderEntity orderEntity){
        QueryWrapper<OrderEntity> orderQueryWrapper = getQueryWrapper(orderEntity);
        return this.page(page, orderQueryWrapper);
    }

    /**
     * 分页查询订单dto
     *
     * @param page 分页条件
     * @param orderEntity 查询条件
     * @return
     */
    @Override
    public Page<OrderDto> queryOrderDtoPage(Page page, OrderEntity orderEntity){
        QueryWrapper<OrderEntity> orderQueryWrapper = getQueryWrapper(orderEntity);
        Page pageInfo = this.page(page, orderQueryWrapper);
        List<OrderDto> orderDtoList = new ArrayList<OrderDto>();
        List<OrderEntity> orderEntityList = pageInfo.getRecords();
        for (int i = 0; i < orderEntityList.size(); i++) {
            OrderDto orderDto = new OrderDto();
            OrderEntity oe = orderEntityList.get(i);
            BeanUtils.copyProperties(oe, orderDto);
            orderDtoList.add(orderDto);
        }
        pageInfo.setRecords(orderDtoList);
        return pageInfo;
    }

    /**
     * 分页查询用户订单你
     *
     * @param page 分页条件
     * @param userId 用户主键
     * @param appId 来源
     * @return
     */
    @Override
    public Page<OrderEntity> queryOrderPage(Page page, String userId, String appId){
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setCreatePersonId(userId);
        orderEntity.setAppId(appId);
        return this.queryOrderPage(page, orderEntity);
    }


    /**
     * 通过条件查询订单
     *
     * @param orderEntity 查询条件
     * @return
     */
    @Override
    public List<OrderEntity> findOrderList(OrderEntity orderEntity){
        QueryWrapper<OrderEntity> orderQueryWrapper = new QueryWrapper<OrderEntity>();
        return this.baseMapper.selectList(orderQueryWrapper);
    }

    /**
     * 通过用户主键查询订单
     *
     * @param page 分页对象
     * @param userId 用户主键
     * @return
     */
    @Override
    public Page<OrderEntity> findByUserIdPage(Page page, String userId){
        QueryWrapper<OrderEntity> orderQueryWrapper = new QueryWrapper<OrderEntity>();
        orderQueryWrapper.orderByDesc("create_time");
        orderQueryWrapper.eq("create_person_id", userId);
        return this.page(page, orderQueryWrapper);
    }

    /**
     * 接口查询订单列表
     *
     * @param page 分页条件
     * @param userId 用户id
     * @param appId 来源
     * @return
     */
    @Override
    public Page<OrderDto> queryIOrderPage(Page page, String userId, String appId){
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setCreatePersonId(userId);
        orderEntity.setAppId(appId);
        QueryWrapper<OrderEntity> queryWrapper = getQueryWrapper(orderEntity);
        return this.page(page, queryWrapper);
    }

}